package spzx.service.impl;

import com.atguigu.spzx.model.dto.product.ProductDto;
import com.atguigu.spzx.model.entity.base.ProductUnit;
import com.atguigu.spzx.model.entity.product.Product;
import com.atguigu.spzx.model.entity.product.ProductDetails;
import com.atguigu.spzx.model.entity.product.ProductSku;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import spzx.mapper.ProductMapper;
import spzx.service.ProductService;

import java.util.Iterator;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;

    @Override
    public PageInfo findByPage(Integer pageNum, Integer pageSize, ProductDto productDto) {
        PageHelper.startPage(pageNum, pageSize);

        List<Product> list = productMapper.selectByCondition(productDto);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public List<ProductUnit> findAllProductUnit() {
        return productMapper.findAllProductUnit();
    }

    @Transactional
    @Override
    public void addProduct(Product product) {
        List<ProductSku> productSkuList = product.getProductSkuList();
        String detailsImageUrls = product.getDetailsImageUrls();

        //设置默认值
        product.setStatus(0);
        product.setAuditStatus(0);

        //添加商品
        productMapper.insertProduct();
        
        //添加product之后，获取其主键
        Long productId = product.getId();
        
        //添加商品sku
        for (int i = 0; i < productSkuList.size(); i++) {
            ProductSku productSku = productSkuList.get(i);

            productSku.setSkuCode(productId + "_" + i);
            productSku.setSkuName(product.getName() + productSku.getSkuSpec()); //sku名称为 商品名称+规格
            productSku.setProductId(productId);
            productSku.setStatus(0);
            productSku.setSaleNum(0);

            productMapper.insertProductSku(productSku);
        }

        //添加Detail
        ProductDetails productDetails = new ProductDetails();
        productDetails.setProductId(productId);
        productDetails.setImageUrls(detailsImageUrls);

        productMapper.insertProductDetails(productDetails);
    }

    @Override
    public void updateProduct(Product product) {
        List<ProductSku> productSkuList = product.getProductSkuList();
        String detailsImageUrls = product.getDetailsImageUrls();
        //1.修改product
        productMapper.updateProduct(product);

        //2.修改productSku
        for(ProductSku productSku : productSkuList){
            productMapper.updateProductSku(productSku);
        }

        //3.修改productDetails
        productMapper.updateProductDetails(detailsImageUrls, product.getId());

    }

    @Override
    public Product findById(Long productId) {
        //根据spu的id,查询spu对象
        Product product = productMapper.getProductById(productId);

        //根据spu的id,查询spu_sku对象
        List<ProductSku> skuList = productMapper.getProductSkuByProductId(productId);

        //根据spu的id,查询spu_details对象
        ProductDetails productDetails = productMapper.getProductDetailsByProductId(productId);

        //将sku列表和details对象封装到product对象中
        product.setProductSkuList(skuList);
        product.setDetailsImageUrls(productDetails.getImageUrls());

        return product;
    }

    @Transactional
    @Override
    public void deleteById(Long spuId) {
        productMapper.deleteProduct(spuId);
        productMapper.deleteProductSku(spuId);
        productMapper.deleteProductDetails(spuId);
    }

    @Override
    public void updateAuditStatus(Long spuId, Integer auditStatus, String reason) {
        productMapper.updateAuditStatus(spuId, auditStatus, reason);
    }

    @Transactional
    @Override
    public void updateStatus(Long spuId, Integer status) {
        productMapper.updateSpuStatus(spuId, status);
        productMapper.updateSkuStatus(spuId, status);
    }
}
